/**
 * 系统管理--用户管理的单例对象
 */
var MgrUser = {
    id: "managerTable",//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    deptid:0
};

/**
 * 初始化表格的列
 */
MgrUser.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true, visible: false},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '登录名', field: 'loginName', align: 'center', valign: 'middle', sortable: true},
        {title: '姓名', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '工号', field: 'userCode', align: 'center', valign: 'middle', sortable: true},
        {title: '性别', field: 'sex', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	return row.sexName;
        }},
        {title: '角色', field: 'roleName', align: 'center', valign: 'middle'},
        {title: '部门', field: 'deptName', align: 'center', valign: 'middle'},
        {title: '职位', field: 'position', align: 'center', valign: 'middle'},
        {title: '创建时间', field: 'createDate', visible: false, align: 'center', valign: 'middle', sortable: true,width:180},
        {title: '状态', field: 'status', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value==1){
                return "<span class='label label-success'>启用</span>";
        	}else{
                return "<span class='label label-warning'>冻结</span>";
        	}
        }},
        {title: '操作', field: 'id', align: 'center', width: 120, valign: 'middle', formatter:function(value,row,index){
        	var _html = '<a href="javascript:MgrUser.openChangeUser(\''+row.id+'\')" title="修改"><i class="fa fa-pencil"></i></a>';
        	_html += '<a href="javascript:MgrUser.resetPwd(\''+row.id+'\')" title="重置密码为123456"><i class="fa fa-undo"></i></a>';
        	if(row.status==1){
        		_html += '<a href="javascript:MgrUser.freezeAccount(\''+row.id+'\')" title="冻结"><i class="fa fa-minus-circle"></i></a>';
        	}else{
        		_html += '<a href="javascript:MgrUser.unfreeze(\''+row.id+'\')" title="解除冻结"><i class="fa fa-check-circle"></i></a>';
        	}
        	_html += '<a href="javascript:MgrUser.delMgrUser(\''+row.id+'\',\''+row.name+'\')" title="删除用户"><i class="fa fa-trash-o"></i></a>';
			return _html;
        }}];
    return columns;
};

/**
 * 检查是否选中
 */
MgrUser.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Sys.info("请先选中表格中的某一记录！");
        return false;
    } else {
        MgrUser.seItem = selected[0];
        return true;
    }
};

/**
 * 点击新增用户
 */
MgrUser.openAddMgr = function () {
	this.layerIndex = Sys.open('新增用户',Sys.backendPath + '/user/to_add', ['800px', '600px']);
};

/**
 * 点击修改按钮时
 * @param userId 用户id
 */
MgrUser.openChangeUser = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
		this.layerIndex = Sys.open('修改用户',Sys.backendPath + '/user/to_edit/' + id, ['800px', '600px']);
    }
};


/**
 * 删除用户
 */
MgrUser.delMgrUser = function (id,name) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        name = this.seItem.name
        flag = true;
    }
	if (flag) {
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/user/delete", function () {
                Sys.success("删除成功!");
                MgrUser.table.refresh();
            }, function (data) {
                Sys.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("userId", id);
            ajax.start();
        };
        Sys.confirm("是否删除用户【" + name + "】?",operation);
    }
};

/**
 * 冻结用户账户
 * @param userId
 */
MgrUser.freezeAccount = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
        var ajax = new $ax(Sys.backendPath + "/user/freeze", function (data) {
            Sys.success("冻结成功!");
            MgrUser.table.refresh();
        }, function (data) {
            Sys.error("冻结失败!" + data.responseJSON.message + "!");
        });
        ajax.set("userId", id);
        ajax.start();
    }
};

/**
 * 解除冻结用户账户
 * @param userId
 */
MgrUser.unfreeze = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
        var ajax = new $ax(Sys.backendPath + "/user/unfreeze", function (data) {
            Sys.success("解除冻结成功!");
            MgrUser.table.refresh();
        }, function (data) {
            Sys.error("解除冻结失败!");
        });
        ajax.set("userId", id);
        ajax.start();
    }
}

/**
 * 重置密码
 */
MgrUser.resetPwd = function (id) {
	var flag = false;
	if(id){
		flag = true;
	}else if (this.check()) {
        id = this.seItem.id;
        flag = true;
    }
	if (flag) {
        Sys.confirm('是否确认重置密码？',function () {
            var ajax = new $ax(Sys.backendPath + "/user/reset", function (data) {
                Sys.success("重置密码成功!");
            }, function (data) {
                Sys.error("重置密码失败!");
            });
            ajax.set("userId", id);
            ajax.start();
        });
    }
};

MgrUser.resetSearch = function () {
    $("#userName").val("");
    $("#beginTime").val("");
    $("#endTime").val("");

    MgrUser.search();
}

MgrUser.search = function () {
    var queryData = {};

    queryData['deptid'] = MgrUser.deptid;
    queryData['userName'] = $("#userName").val();
    queryData['beginTime'] = $('#beginTime').val();
    queryData['endTime'] = $('#endTime').val();
    MgrUser.table.refresh({query: queryData});
}

MgrUser.onClickDept = function (e, treeId, treeNode) {
    MgrUser.deptid = treeNode.id;
    $("#deptid").val(treeNode.id);
    MgrUser.search();
};

MgrUser.expUsers = function () {
    var $df = $("#downForm");
    //提交表单，实现下载
    $df.submit();
};

MgrUser.impUsers = function () {
    var index = layer.open({
        type: 2,
        title: '导入用户',
        area: ['700px', '300px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Sys.backendPath + '/user/to_import'
    });
    this.layerIndex = index;
};

MgrUser.template = function () {
	window.location.href = Sys.backendPath+"/static/backend/system/user/用户导入模板.xlsx";
};


$(function () {
	$('.hisui-datebox').datebox({});    //重新渲染datebox -不渲染图标左右间距不等
    var defaultColunms = MgrUser.initColumn();
    var table = new BSTable("managerTable", Sys.backendPath + "/user/list", defaultColunms);
    table.setPaginationType("server");
    table.setSort("weight");
    table.setSubHeight(-5);
    MgrUser.table = table.init();
    var ztree = new $ZTree("deptTree", Sys.backendPath + "/dept/tree");
    ztree.bindOnClick(MgrUser.onClickDept);
    ztree.init();
    MgrUser.ztree = ztree;
    fuzzySearch('deptTree','#seachKey',null,true);
});
